package com.mentaleye.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mentaleye.bean.DeptBean;
import com.mentaleye.dal.dao.DeptDao;
import com.mentaleye.dal.model.Dept;
import com.mentaleye.dal.model.DeptExample;
import com.mentaleye.service.DeptService;
import com.mentaleye.util.ObjectUtil;

@Service
public class DeptServiceImpl implements DeptService{

	@Autowired private DeptDao deptDao;
	
	@Override
	public boolean addDept(DeptBean deptBean){
		return deptDao.insertDept((Dept)ObjectUtil.transfer(deptBean, Dept.class));
	}
	
	@Override
	public boolean modifyDept(DeptBean deptBean){
		return deptDao.updateDept((Dept)ObjectUtil.transfer(deptBean, Dept.class));
	}
	
	@Override
	public boolean removeDept(Integer deptId){
		if(deptId != null){
			return deptDao.deleteDept(deptId);
		}
		return false;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<DeptBean> queryDeptAll(){
		return ObjectUtil.transferList(deptDao.selectByExample(null), DeptBean.class);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<DeptBean> queryDeptByUpDeptId(Integer upDeptId){
		DeptExample example = new DeptExample();
		example.createCriteria().andUpDeptIdEqualTo(upDeptId);
		return ObjectUtil.transferList(deptDao.selectByExample(example), DeptBean.class);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<DeptBean> queryByUrlLike(String deptUrl){
		DeptExample example = new DeptExample();
		example.createCriteria().andDeptUrlLike(deptUrl);
		return ObjectUtil.transferList(deptDao.selectByExample(example), DeptBean.class);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<DeptBean> queryByUpDeptIdAndDeptName(int upDeptId,String deptName){
		DeptExample example = new DeptExample();
		example.createCriteria().andUpDeptIdEqualTo(upDeptId);
		example.createCriteria().andDeptNameEqualTo(deptName);
		return ObjectUtil.transferList(deptDao.selectByExample(example), DeptBean.class);
	}
	
	@Override
	public DeptBean queryDeptByDeptId(Integer deptId){
		return (DeptBean) ObjectUtil.transfer(deptDao.selectByDeptId(deptId), DeptBean.class);
	}
}
